package com.cn.boot.student.service.team.impl;

import com.cn.boot.student.entity.TeamUserRelationEntity;
import com.cn.boot.student.entity.UserEntity;
import com.cn.boot.student.mapper.DispatchRecordMapper;
import com.cn.boot.student.mapper.TeamUserRelationMapper;
import com.cn.boot.student.service.common.impl.RedisServiceImpl;
import com.cn.boot.student.service.team.ITeamUserRelationService;
import com.cn.boot.student.util.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 小组用户关系服务逻辑层
 */
@Service
@CacheConfig(cacheNames = "sys:tur")
public class TeamUserRelationServiceImpl implements ITeamUserRelationService {
    /**
     * 注入数据操作接口
     */
    @Autowired
    private TeamUserRelationMapper teamUserRelationMapper;
    /**
     * 注入调度记录数据操作接口
     */
    @Autowired
    private DispatchRecordMapper dispatchRecordMapper;
    /**
     * 注入缓存工具类
     */
    @Autowired
    private RedisServiceImpl redisService;

    /**
     * 根据小组id得到小组成员
     *
     * @param id
     * @return
     */
    @Override
    public CommonResult getStudentByTeamId(Integer id) {
        List<UserEntity> list = teamUserRelationMapper.getStudentByTeamId(id);
        if (list.size() == 0) {
            return CommonResult.failed("小组暂无成员，请分配成员！！！");
        }
        return CommonResult.success(list, "查询成功！！！");
    }

    /**
     * 分配小组成员
     *
     * @param entity
     * @return
     */
//    @Override
//    public CommonResult assignStudent(TeamUserRelationEntity entity) {
//        Integer result = teamUserRelationMapper.add(entity);
//        if (result == 0) {
//            return CommonResult.failed("分配失败，请重新操作！！！");
//        }
//
//        return CommonResult.success(result, "分配成功！！！");
//    }

    /**
     * 分配小组成员(批量分配)
     *
     * @param entityList
     * @return
     */
    @Override
    public CommonResult assignStudents(List<TeamUserRelationEntity> entityList) {
        entityList.stream().forEach(teamUserRelationMapper::add);
        if (entityList.size() == 0) {
            return CommonResult.failed("分配失败，请重新操作！！！");
        }
        //添加滴入日志记录
        entityList.stream().forEach(dispatchRecordMapper::adds);
        return CommonResult.success(entityList.size(), "成功分配" + entityList.size() + "名组员");

    }

    /**
     * 删除小组成员(单个)
     *
     * @param entity
     * @return
     */
//    @Override
//    public CommonResult del(TeamUserRelationEntity entity) {
//        Integer result = teamUserRelationMapper.del(entity);
//        if (result == 0) {
//            return CommonResult.failed("删除失败，请重新操作！！！");
//        }
//
//        return CommonResult.success(result, "删除成功！！！");
//    }

    /**
     * 删除小组成员(批量)
     *
     * @param entityList
     * @return
     */
    @Override
    public CommonResult dels(List<TeamUserRelationEntity> entityList) {
        entityList.stream().forEach(teamUserRelationMapper::del);
        if (entityList.size() == 0) {
            return CommonResult.failed("删除失败，请重新操作！！！");
        }
        //添加调离日志记录
        entityList.stream().forEach(dispatchRecordMapper::dels);
        return CommonResult.success(entityList.size(), "成功删除" + entityList.size() + "名组员");
    }
}
